package com.xthh._function;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.xt.xthh.R;
import com.xthh._struct.HH_T_BindDevice;
import com.xthh._struct.HH_T_BulkAdd;
import com.xthh._struct.HH_T_BulkImportLayerTagInfo;
import com.xthh._struct.HH_T_DeviceModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class HH_ControlXLS {


    public static List<HH_T_BulkAdd> GetBulkAddDeviceInfoFormXls(Context context, String path){
        List<HH_T_BulkAdd> list = new ArrayList<>();

        try {
            File file = new File(path);
            Workbook book = Workbook.getWorkbook(file);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();
            int Cols = sheet.getColumns();

            for (int i = 1; i < Rows; ++i) {
                String mac = (sheet.getCell(0, i)).getContents();
                String name = (sheet.getCell(1, i)).getContents();

                list.add(new HH_T_BulkAdd(mac, name));
            }
            book.close();

        } catch (Exception e) {

            Log.e("FileUtil", "e" + e);
        }

        return list;
    }

    public static String WriteBulkAddLayerinfoTemplateFile(Context ctx, HH_T_DeviceModel td, int deviceCount, String bz, List<HH_T_BindDevice> list){

        if (list == null || list.size() != deviceCount)
            return "";

        String rootDataDir = Environment.getExternalStorageDirectory().getPath();
        String savePath = String.format("%s%s%s%s",
                rootDataDir,
                File.separator,
                "xtfiles",
                File.separator);
        HH_FileUtil.CreateFolder(savePath);

        savePath += HH_TimeUtil.GetCurrentSystemTimeF();
        savePath += "_";
        savePath += ((bz == null || bz.length() <= 0) ? "" : bz);
        savePath += ".xls";


        initFormat();
        WritableWorkbook workbook = null;
        try {
            File file = new File(savePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            workbook = Workbook.createWorkbook(file);
            WritableSheet sheet = workbook.createSheet(ctx.getString(R.string.strID_236), 0);

            for (int i = 0; i < deviceCount; i++){
                sheet.addCell(new Label(i + 1, 0, String.format("Tag%d-%s", i + 1, list.get(i).getMacRes()), titleFormat));
            }

            for (int i = 0; i < td.getListLayerInfo().size(); i++){
                sheet.addCell(new Label(0, i + 1, td.getListLayerInfo().get(i).getLayerName(), contentFormat));

                for (int j = 0; j < deviceCount; j++){
                    sheet.addCell(new Label(j + 1, i + 1, String.format("%s-%02d", td.getListLayerInfo().get(i).getText(), j + 1) , contentFormat));
                }
            }

            workbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return savePath;
    }

    public static List<HH_T_BulkImportLayerTagInfo> GetBulkLayerInfoFormXls(Context context, String path, int tagCount){
        List<HH_T_BulkImportLayerTagInfo> tagList = new ArrayList<>();

        try {
            File file = new File(path);
            Workbook book = Workbook.getWorkbook(file);
            book.getNumberOfSheets();
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();
            int Cols = sheet.getColumns();

            if (tagCount > 0){
                for (int col = 0; col < tagCount; col++){
                    if (col < Cols){
                        HH_T_BulkImportLayerTagInfo tbl = new HH_T_BulkImportLayerTagInfo();
                        List<String> layerinfo = new ArrayList<>();
                        for (int i = 1; i < Rows; ++i) {
                            String temp = (sheet.getCell(col + 1, i)).getContents();
                            layerinfo.add(temp);
                        }
                        tbl.setLayerinfo(layerinfo);

                        tagList.add(tbl);
                    }
                }
            } else {
                for (int col = 0; col < Cols; col++){
                    HH_T_BulkImportLayerTagInfo tbl = new HH_T_BulkImportLayerTagInfo();
                    List<String> layerinfo = new ArrayList<>();
                    for (int i = 1; i < Rows; ++i) {
                        String temp = (sheet.getCell(col + 1, i)).getContents();
                        layerinfo.add(temp);
                    }
                    tbl.setLayerinfo(layerinfo);
                    tagList.add(tbl);
                }
            }

            book.close();

        } catch (Exception e) {

            Log.e("FileUtil", "e" + e);
        }

        return tagList;
    }

    private static WritableCellFormat fileNameFormat = null;
    private static WritableCellFormat titleFormat = null;
    private static WritableCellFormat contentFormat = null;

    // 设置格式字体等参数
    private static void initFormat() {
        try {
            WritableFont font1 = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
            fileNameFormat = new WritableCellFormat(font1);
            fileNameFormat.setAlignment(jxl.format.Alignment.CENTRE);
            fileNameFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            fileNameFormat.setBackground(Colour.VERY_LIGHT_YELLOW);
            WritableFont font2 = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
            titleFormat = new WritableCellFormat(font2);
            titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
            titleFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            titleFormat.setBackground(Colour.GRAY_25);
            WritableFont font3 = new WritableFont(WritableFont.ARIAL, 10);
            contentFormat = new WritableCellFormat(font3);
            contentFormat.setAlignment(jxl.format.Alignment.CENTRE);//居中
            contentFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //边框
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }
}
